﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CNotebook;

namespace NotebookS2
{
    public partial class CRYSTAL_DR_LOG : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public CRYSTAL_DR_LOG()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public DateTime FromDate { get; set; }
        public DateTime ToDate { get; set; }

        public bool WithDraft { get; set; }
        public bool WithDelivered { get; set; }
        public bool WithPosted { get; set; }
        public bool WithOpen { get; set; }
        public bool WithClosed { get; set; }

        ReportDocument rptDoc = new ReportDocument();
        SqlConnection oConnection = new SqlConnection();
        SqlCommand oCommand = new SqlCommand();
        SqlDataAdapter oAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();

        public string GetLayout()
        {
            ReportConfigManager oManager = new ReportConfigManager();
            const int _DOCUMENT_TYPE = 43;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetReportFileName(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        public void ViewSalesOrderLogs()
        {
            try
            {
                rptDoc = new ReportDocument();
                oConnection = new SqlConnection();
                oCommand = new SqlCommand();
                oAdapter = new SqlDataAdapter();
                ds = new DataSet();

                string sReportFile = "";

                sReportFile = GetLayout();

                rptDoc.Load(sReportFile);





                oConnection.ConnectionString = this.ConnectionString;
                oConnection.Open();

                oCommand.Connection = oConnection;
                oCommand.CommandType = CommandType.StoredProcedure;

                oCommand.CommandText = "RPT_DR_LOG";


                oCommand.Parameters.Add(new SqlParameter("@FROM", this.FromDate));
                oCommand.Parameters.Add(new SqlParameter("@TO", this.ToDate));

                oCommand.Parameters.Add(new SqlParameter("@WITHDRAFT", Convert.ToInt16(this.WithDraft)));
                oCommand.Parameters.Add(new SqlParameter("@WITHDELIVERED", Convert.ToInt16(this.WithDelivered)));
                oCommand.Parameters.Add(new SqlParameter("@WITHPOSTED", Convert.ToInt16(this.WithPosted)));
                oCommand.Parameters.Add(new SqlParameter("@WITHOPEN", Convert.ToInt16(this.WithOpen)));
                oCommand.Parameters.Add(new SqlParameter("@WITHCLOSED", Convert.ToInt16(this.WithClosed)));

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                oConnection.Close();

                rptDoc.SetDataSource(ds.Tables[0]);

                ParameterField oFrom = new ParameterField();
                ParameterField oTo = new ParameterField();



                //ParameterFields oFields = new ParameterFields();
                //rptDoc.SetParameterValue(0, this.FromDate);
                //rptDoc.SetParameterValue(1, this.ToDate);

                //rptDoc.SetParameterValue(2, Convert.ToInt16(this.WithDraft));
                //rptDoc.SetParameterValue(3, Convert.ToInt16(this.WithPosted));
                //rptDoc.SetParameterValue(4, Convert.ToInt16(this.WithOpen));
                //rptDoc.SetParameterValue(5, Convert.ToInt16(this.WithClosed));

                CRViewer.ReportSource = rptDoc;
                CRViewer.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void CRYSTAL_SALESORDER_Load(object sender, EventArgs e)
        {
            ViewSalesOrderLogs();
        }

        private void CRYSTAL_DR_LOG_FormClosing(object sender, FormClosingEventArgs e)
        {
            rptDoc.Dispose();
            oConnection.Dispose();
            oCommand.Dispose();
            oAdapter.Dispose();
            ds.Dispose();
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }
    }
}
